/*
 * @Author: ydli
 * @LastEditTime: 2021-05-02 20:09:09
 * @Description: 
 */
import { ColumnProps } from './store';

export function generatefitUrl(column: ColumnProps, width: number, height: number):void {
    if (column.avatar) {
        column.avatar.fitUrl = column.avatar.url + `?x-oss-process=image/resize,h_${height},w_${width}`
    } else {
        column.avatar = {
            fitUrl: require('@/assets/logo.png')
        }
    }
}

interface CheckCondition {
    format?:string[];
    size?:number;
}

type ErrorType = 'size' | 'format' | null;


interface BeforeCheck {
    passed:boolean;
    error?:ErrorType
}
// 检查函数
export function beforeUploadCheck(file:File,condition:CheckCondition):BeforeCheck{
    const {format,size} = condition;
    const isValidFormat = format ? format.includes(file.type) : true;
    const isValidSize = size ? (file.size / 1024 / 1024 < size) : true;
    let error:ErrorType = null;
    if(!isValidFormat){
        error = 'format';
    }
    if(!isValidSize){
        error = 'size';
    }
    return {
        passed:isValidFormat && isValidSize,
        error
    }
}